package com.cloudree.gllib.frament.cube;


import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.cloudree.gllib.BaseActivity;
import com.cloudree.gllib.adapter.FilterAdapter;
import com.cloudree.gllib.bean.FilterBean;
import com.cloudree.gllib.constants.Filter;
import com.cloudree.gllib.frament.BaseFragment;
import com.cloudree.gllib.render.FilterRender;
import com.cloudree.gllib.render.TriangleRender;
import com.cloudree.gllib.surface.BaseGLSurface;
import com.kvin.toolkit.utils.LogUtils;
import com.kvin.toolkit.utils.MetricsUtils;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class FilterFragment extends BaseFragment {
    private float density;

    private float previousX;
    private float previousY;

    private FilterRender mRender;
    private DisplayMetrics metrics;
    private final float clickWidth = 150.0f;

    private AlertDialog mDialog;
    private ArrayList<FilterBean> filters;
    private FilterAdapter mAdapter;
    private FilterBean bean;
    private boolean isMinFilter;

    public FilterFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mSurface = new BaseGLSurface(getActivity());
        mRender = new FilterRender();
        mSurface.setRenderer(mRender);
        ((BaseActivity)getActivity()).setTouchListener(this);
        return mSurface;
//        return inflater.inflate(R.layout.fragment_triangle, container, false);
    }

    @Override
    public void init() {
        metrics = MetricsUtils.winMetrics(getActivity());
        density = metrics.density;
        initDialog();
    }


    @Override
    public void initDialog() {
        filters = new ArrayList<>();
        mAdapter = new FilterAdapter(filters, getActivity());
        AlertDialog.Builder mBuilder = new AlertDialog.Builder(getActivity());
        mBuilder.setTitle("filter choose:");
        mBuilder.setAdapter(mAdapter, mItemClickListener);
        mDialog = mBuilder.create();
    }


    private DialogInterface.OnClickListener mItemClickListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int position) {
            bean = filters.get(position);
            mSurface.queueEvent(new Runnable() {
                @Override
                public void run() {
                    if (isMinFilter)
                        mRender.setMinFilter(bean.getId());
                    else
                        mRender.setMagfilter(bean.getId());
                }
            });
        }
    };

    @Override
    public boolean onTouch(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        LogUtils.d("------w=" + metrics.widthPixels + ":x=" + x);
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            mRender.mDeltaX += (x - previousX) / density / 2;
            mRender.mDeltaY += (y - previousY) / density / 2;
        } else if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (x < clickWidth) {
                //set min filter
                isMinFilter = true;
                filters.clear();
                filters.addAll(Filter.minFilters());
                mAdapter.notifyDataSetChanged();
                mDialog.show();
            } else if (x + clickWidth > metrics.widthPixels) {
                //set mag filter
                isMinFilter = false;
                filters.clear();
                filters.addAll(Filter.magFilters());
                mAdapter.notifyDataSetChanged();
                mDialog.show();
            }
        }
        previousX = x;
        previousY = y;
        return super.onTouch(event);
    }
}
